package pl.edu.pb.wi.cookbook.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import pl.edu.pb.wi.cookbook.session.UserSession;

public class UserInterceptor extends HandlerInterceptorAdapter {
    private UserSession userSession;

    @Autowired
    public UserInterceptor(UserSession userSession) {
        this.userSession = userSession;
    }

    @Override
    public boolean preHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler) throws Exception {
        if (userSession.getUser() == null) {
            response.sendRedirect(request.getContextPath() + "/main");
            return false;
        } else {
            return super.preHandle(request, response, handler);
        }
    }
}
